package cur.nature.framework.core.util;

import cn.hutool.core.util.StrUtil;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Map;

public class StringUtil  extends StrUtil {

    public  static String convertArrayToStr(Collection target){
        if(null == target){return null;}
        String res = JsonUtil.toJSONString(target);
        return res.substring(1, res.length()-1).replace("\"", "'");
    }
    public static String replaceAll(String condition, Map<String,Object> param){
        if(condition != null && param != null && param.size() > 0){
            for (Map.Entry<String, Object> entry : param.entrySet()) {
                condition = condition.replaceAll(entry.getKey(), String.valueOf(entry.getValue()));
            }
        }
        return condition;
    }
    public static String replaceAll(String condition, String regex, String defaultValue){
        if(condition != null && defaultValue != null){
            condition = condition.replaceAll(regex, defaultValue);
        }
        return condition;
    }

    public static String format(String format, final Object... args) {
        format = MessageFormat.format(format,  args);
        return format;
    }

    public static String nullStr(String str) {
        return str == null ? "" : str;
    }

    public static String unicodeToString(String unicodeStr){
        if (isEmpty(unicodeStr)) {return unicodeStr;}
        StringBuilder sb = new StringBuilder();
        int i = -1;
        int pos = 0;
        //在pos及之后搜索\\u
        while((i = unicodeStr.indexOf("\\u", pos)) != -1) {
            sb.append(unicodeStr.substring(pos, i));

            if((i+5) < unicodeStr.length()){
                String unicode = unicodeStr.substring(i + 2, i + 6);
                sb.append((char) Integer.parseInt(unicode, 16));
                pos = i + 6;
            } else {
                sb.append(unicodeStr.substring(i, unicodeStr.length()));
                return sb + "";
            }
        }
        return sb + "" + unicodeStr.substring(pos, unicodeStr.length());
    }
}
